package com.jungle.mvvm.sample.app;

import com.jungle.mvvm.net.BaseObserver;
import com.jungle.mvvm.sample.bean.BaseBean;
import com.jungle.mvvm.sample.bean.ListBean;
import com.jungle.mvvm.sample.bean.SameBean;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2021/1/18 14:47
 */
public abstract class WanObserver<T> extends BaseObserver<T> {
    @Override
    public void onSuccess(T t) {
        if (t instanceof ListBean) {
            ListBean listBean = (ListBean) t;
            if (listBean.getErrorCode() == 0) {
                onWanSuccess(t);
            } else {
                onWanFailure(listBean.getErrorMsg());
            }
        } else if (t instanceof SameBean) {
            SameBean wanBean = (SameBean) t;
            if (wanBean.getErrorCode() == 0) {
                onWanSuccess(t);
            } else {
                onWanFailure(wanBean.getErrorMsg());
            }
        } else if (t instanceof BaseBean) {
            BaseBean baseBean = (BaseBean) t;
            if (baseBean.getErrorCode() == 0) {
                onWanSuccess(t);
            } else {
                onWanFailure(baseBean.getErrorMsg());
            }
        } else {
            onWanSuccess(t);
        }
    }

    @Override
    public void onFailure(Throwable e) {
        onWanFailure(e.getMessage());
    }

    /**
     * 成功回调
     *
     * @param t 对象
     */
    public abstract void onWanSuccess(T t);

    /**
     * 失败回调
     *
     * @param e 信息
     */
    public abstract void onWanFailure(String e);
}
